package ArrayList;

//自己实现一个简单的顺序表
public class MyArrayList {

    private int arr[];
    private int size = 0;
    ////////////////////////////////////////////////////////////////
    //正式代码
    ///////////////////////////////////////////////////////////////


    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < size; i++) {
            stringBuilder.append(arr[i]);
            if (i<size-1){
//                System.out.println(",");
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
    public MyArrayList(){
        arr = new int[10];
    }

    public MyArrayList(int capacity) {
        arr = new int[capacity];
    }

    public int size(){
        return size;
    }

    public void add(int val) {
        if (size == arr.length){
            resize();
        }
            arr[size] = val;
            size++;

    }

    public void add(int index,int val) {
        if (index < 0 || index >size){
            throw new IndexOutOfBoundsException("Index out of bounds!");
        }
        if (size == arr.length){
            resize();
        }
        for (int i = size; i >index ; i++) {
            arr[i+1] = arr[i];
        }
        arr[index] = val;
        size++;
    }

    private void resize(){
        int[] newArr = new int[(int)(arr.length*1.5)];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        arr = newArr;
    }

    public int get(int index){
        if (index < 0 || index >size){
            throw new IndexOutOfBoundsException("Index out of bounds!");
        }
        return arr[index];
    }
    public void set(int index , int val){
        if (index < 0 || index >size){
            throw new IndexOutOfBoundsException("Index out of bounds!");
        }
        arr[index] = val;
    }

    public int remove(int index){
        if (index < 0 || index >size){
            throw new IndexOutOfBoundsException("下标越界：" + index);
        }
        int result = arr[index];
        for (int i = index; i < size-1; i++) {
            arr[i] = arr[i+1];
        }
        size--;
        return result;

    }
//    public void delete(int val){
//        int index = 0;
//        for (; index < size-1; index++) {
//            if(arr[index] == val){
//                break;
//            }
//        }
//        if (index == size){
//            System.out.println("元素不存在！");
//        }
//        for (int i = index; i < size-1; i++) {
//            arr[i] = arr[i+1];
//        }
//        size--;
//    }

    public void delete(int val){
        int index = indexOf(val);
        if (index == -1){
            return;
        }
        remove(index);
    }

    public boolean contains(int val){
        for (int i = 0; i < size; i++) {
            if (arr[i] == val){
                return true;
            }
        }
        return false;
    }

    public int indexOf(int val){
        for (int i = 0; i < size; i++) {
            if (arr[i] == val){
                return i;
            }
        }
        return -1;
    }

    public void clear(){
        size = 0;
    }

    ////////////////////////////////////////////////////////////////
    //测试代码
    ///////////////////////////////////////////////////////////////

    private static void test01(){
        MyArrayList list = new MyArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);
        list.add(11);
        list.add(12);
        System.out.println(list.size());
        System.out.println(list.toString());
    }
    private static void test02(){
        MyArrayList list = new MyArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);
        list.add(11);
        list.add(12);
        System.out.println(list.get(10));
        list.set(6,99);
        int result = list.remove(11);
        System.out.println(result);
        System.out.println(list.size());
        System.out.println(list.toString());
    }
    private static void test03(){
        MyArrayList list = new MyArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(3);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);
        list.add(11);
        list.add(12);
        list.delete(3);
        System.out.println(list.contains(3));
        System.out.println(list.contains(87));
        System.out.println(list.toString());
    }

    public static void main(String[] args) {
//        test01();
//        test02();
        test03();
    }
}

